﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.Presenter.Dictionary.Account;
using WindowsForms.AOnline.Presenter.Dictionary.FixedAssetCategory;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.WindowsForm.FormBase;
using WindowsForms.AOnline.WindowsForm.FormBase.PropertyGrid;
using WindowsForms.AOnline.WindowsForm.Resources;

namespace WindowsForms.AOnline.WindowsForm.FormDictionary
{
    public partial class FrmXtraFixedAssetCategoryDetail : FrmXtraBaseTreeDetail, IFixedAssetCategoryView, IFixedAssetCategorysView, IAccountsView
    {
        /// <summary>
        /// The _fixed asset category presenter
        /// </summary>
        private readonly FixedAssetCategoryPresenter _fixedAssetCategoryPresenter;

        /// <summary>
        /// The _fixed asset categories presenter
        /// </summary>
        private readonly FixedAssetCategorysPresenter _fixedAssetCategoriesPresenter;

        /// <summary>
        /// The _accounts presenter
        /// </summary>
        private readonly AccountsPresenter _accountsPresenter;

        /// <summary>
        /// Initializes a new instance of the <see cref="FrmXtraFixedAssetCategoryTreeDetail"/> class.
        /// </summary>

        #region IFixedAssetCategoryView Members

        /// <summary>
        /// Gets or sets the fixed asset category identifier.
        /// </summary>
        /// <value>
        /// The fixed asset category identifier.
        /// </value>
        public int FixedAssetCategoryId { get; set; }

        /// <summary>
        /// Gets or sets the parent identifier.
        /// </summary>
        /// <value>
        /// The parent identifier.
        /// </value>
        public int? ParentId
        {
            get
            {
                if (grdLockUpParentID.EditValue == null) return null;
                return (int)grdLockUpParentID.EditValue;
            }
            set
            {
                grdLockUpParentID.EditValue = value;
            }
        }

        /// <summary>
        /// Gets or sets the fixed asset category code.
        /// </summary>
        /// <value>
        /// The fixed asset category code.
        /// </value>
        public string FixedAssetCategoryCode
        {
            get { return txtFixedAssetCategoryCode.Text; }
            set { txtFixedAssetCategoryCode.Text = value; }
        }

        /// <summary>
        /// Gets or sets the name of the fixed asset category.
        /// </summary>
        /// <value>
        /// The name of the fixed asset category.
        /// </value>
        public string FixedAssetCategoryName
        {
            get
            {
                return txtFixedAssetCategoryName.Text;
            }
            set
            {
                txtFixedAssetCategoryName.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the depreciation rate.
        /// </summary>
        /// <value>
        /// The depreciation rate.
        /// </value>
        public decimal DepreciationRate
        {
            get
            {
                return txtDepreciationRate.Value;
            }
            set
            {
                txtDepreciationRate.Value = value;
            }
        }

        /// <summary>
        /// Gets or sets the life time.
        /// </summary>
        /// <value>
        /// The life time.
        /// </value>
        public decimal LifeTime
        {
            get
            {
                return txtLifeTime.Value;
            }
            set
            {
                txtLifeTime.Value = value;
            }
        }

        /// <summary>
        /// Gets or sets the grade.
        /// </summary>
        /// <value>
        /// The grade.
        /// </value>
        public int Grade { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [is parent].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is parent]; otherwise, <c>false</c>.
        /// </value>
        public bool IsParent { get; set; }

        /// <summary>
        /// Gets or sets the org price account.
        /// </summary>
        /// <value>
        /// The org price account.
        /// </value>
        public string OrgPriceAccountCode
        {
            get
            {
                if (grdLookUpOrgPriceAccount.EditValue == null) return null;
                return (string)grdLookUpOrgPriceAccount.GetColumnValue("AccountCode");
            }
            set
            {
                grdLookUpOrgPriceAccount.EditValue = value;
            }
        }

        /// <summary>
        /// Gets or sets the depreciation account.
        /// </summary>
        /// <value>
        /// The depreciation account.
        /// </value>
        public string DepreciationAccountCode
        {
            get
            {
                if (grdLookUpDepreciationAccount.EditValue == null) return null;
                return (string)grdLookUpDepreciationAccount.GetColumnValue("AccountCode");
            }
            set
            {
                grdLookUpDepreciationAccount.EditValue = value;
            }
        }

        /// <summary>
        /// Gets or sets the capital account.
        /// </summary>
        /// <value>
        /// The capital account.
        /// </value>
        public string CapitalAccountCode
        {
            get
            {
                if (grdLookUpCapitalAccount.EditValue == null) return null;
                return (string)grdLookUpCapitalAccount.GetColumnValue("AccountCode");
            }
            set
            {
                grdLookUpCapitalAccount.EditValue = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [in active].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [in active]; otherwise, <c>false</c>.
        /// </value>
        public bool IsActive
        {
            get
            {
                return chkInactive.Checked;
            }
            set
            {
                chkInactive.Checked = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [in active].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [in active]; otherwise, <c>false</c>.
        /// </value>
        public long UnitId{get; set; }

        #endregion

        #region IFixedAssetCategoriesView Members

        /// <summary>
        /// Sets the fixed asset categorys.
        /// </summary>
        /// <value>
        /// The fixed asset categorys.
        /// </value>
        public IList<FixedAssetCategoryModel> FixedAssetCategorys
        {
            set
            {
                grdLockUpParentID.Properties.DataSource = value;
                grdLockUpParentID.Properties.ForceInitialize();
                //grdLockUpParentIDView.PopulateColumns(value);
                grdLockUpParentID.Properties.PopulateColumns();

                var treeColumnsCollection = new List<XtraColumn>
                                            {
                                                new XtraColumn
                                                {
                                                    ColumnName = "FixedAssetCategoryId",
                                                    ColumnCaption = "Mã loại TS",
                                                    ColumnVisible = false,
                                                    ColumnWith = 100
                                                },
                                                new XtraColumn
                                                {
                                                    ColumnName = "ParentId",
                                                    ColumnCaption = "Tài khoản cha",
                                                    ColumnVisible = false
                                                },
                                                new XtraColumn
                                                {
                                                    ColumnName = "FixedAssetCategoryCode",
                                                    ColumnCaption = "Mã loại TS",
                                                    ColumnPosition = 1,
                                                    ColumnVisible = true
                                                },
                                                new XtraColumn
                                                {
                                                    ColumnName = "FixedAssetCategoryName",
                                                    ColumnCaption = "Tên loại TS",
                                                    ColumnPosition = 2,
                                                    ColumnVisible = true
                                                },
                                                new XtraColumn
                                                {
                                                    ColumnName = "IsActive",
                                                    ColumnCaption = "Ngừng sử dụng",
                                                    ColumnVisible = false
                                                },
                                                new XtraColumn
                                                {
                                                    ColumnName = "LifeTime",
                                                    ColumnPosition = 3,
                                                    ColumnCaption = "Thời gian sử dụng",
                                                    ColumnWith = 100,
                                                    ColumnVisible = true
                                                },
                                                new XtraColumn
                                                {
                                                    ColumnName = "DepreciationRate",
                                                    ColumnPosition = 4,
                                                    ColumnCaption = "Tỷ lệ khấu hao",
                                                    ColumnWith = 100,
                                                    ColumnVisible = true
                                                },
                                                new XtraColumn { ColumnName = "Grade", ColumnVisible = false },
                                                new XtraColumn { ColumnName = "IsParent", ColumnVisible = false },
                                                new XtraColumn { ColumnName = "OrgPriceAccountCode", ColumnVisible = false},
                                                new XtraColumn { ColumnName = "DepreciationAccountCode", ColumnVisible = false },
                                                new XtraColumn { ColumnName = "OrgPriceAccountCode", ColumnVisible = false },
                                                new XtraColumn { ColumnName = "CapitalAccountCode", ColumnVisible = false }
                                            };

                foreach (var column in treeColumnsCollection)
                {
                    if (column.ColumnVisible)
                    {
                        grdLockUpParentID.Properties.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLockUpParentID.Properties.SortColumnIndex = column.ColumnPosition;
                    }
                    else grdLockUpParentID.Properties.Columns[column.ColumnName].Visible = false;
                }
                grdLockUpParentID.Properties.DisplayMember = "FixedAssetCategoryCode";
                grdLockUpParentID.Properties.ValueMember = KeyFieldName;
            }
        }

        #endregion

        #region IAccountsView Members

        /// <summary>
        /// Sets the accounts.
        /// </summary>
        /// <value>
        /// The accounts.
        /// </value>
        public IList<AccountModel> Accounts
        {
            set
            {
                var capitalAccountList = value.Where(c => c.AccountCode.StartsWith("466")).ToList();
                grdLookUpCapitalAccount.Properties.DataSource = capitalAccountList;
                grdLookUpCapitalAccount.Properties.ForceInitialize();
                grdLookUpCapitalAccount.Properties.PopulateColumns();

                var depreciationAccountList = value.Where(c => c.AccountCode.StartsWith("214")).ToList();
                grdLookUpDepreciationAccount.Properties.DataSource = depreciationAccountList;
                grdLookUpDepreciationAccount.Properties.ForceInitialize();
                grdLookUpDepreciationAccount.Properties.PopulateColumns();

                var orgPriceAccountList = value.Where(c => c.AccountCode.StartsWith("211")).ToList();
                grdLookUpOrgPriceAccount.Properties.DataSource = orgPriceAccountList;
                grdLookUpOrgPriceAccount.Properties.ForceInitialize();
                grdLookUpOrgPriceAccount.Properties.PopulateColumns();

                var treeColumnsCollection = new List<XtraColumn>
                                            {
                                                new XtraColumn
                                                {
                                                    ColumnName = "AccountId", ColumnCaption = "", ColumnVisible = false
                                                },
                                                  new XtraColumn
                                                {
                                                    ColumnName = "UnitId", ColumnCaption = "", ColumnVisible = false
                                                },
                                                  new XtraColumn
                                                {
                                                    ColumnName = "AccountCode", ColumnPosition = 1, ColumnCaption = "Mã tài khoản", ColumnVisible = true 
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "AccountName", ColumnPosition = 2, ColumnCaption = "Tên tài khoản", ColumnVisible = true 
                                                },
                                                
                                                 new XtraColumn
                                                {
                                                    ColumnName = "ParentId", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "Grade", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsDetail", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "Description", ColumnCaption = "", ColumnVisible = false
                                                },
                                             
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsCustomer", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsVendor", ColumnCaption = "", ColumnVisible = false
                                                },
                                                
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsInventoryItem", ColumnCaption = "", ColumnVisible = false
                                                },
                                                  new XtraColumn
                                                {
                                                    ColumnName = "IsFixedAsset", ColumnCaption = "", ColumnVisible = false
                                                },                                              
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsSystem", ColumnCaption = "", ColumnVisible = false
                                                },
                                                 new XtraColumn
                                                {
                                                    ColumnName = "IsTax", ColumnCaption = "", ColumnVisible = false
                                                },
                                                new XtraColumn
                                                {
                                                    ColumnName = "IsActive",   ColumnPosition = 3,ColumnCaption = "Được sử dụng",  ColumnVisible = false                                            
                                                    
                                                }
                                            };

                foreach (var column in treeColumnsCollection)
                {
                    if (column.ColumnVisible)
                    {
                        grdLookUpCapitalAccount.Properties.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLookUpCapitalAccount.Properties.SortColumnIndex = column.ColumnPosition;

                        grdLookUpDepreciationAccount.Properties.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLookUpDepreciationAccount.Properties.SortColumnIndex = column.ColumnPosition;

                        grdLookUpOrgPriceAccount.Properties.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLookUpOrgPriceAccount.Properties.SortColumnIndex = column.ColumnPosition;
                    }
                    else
                    {
                        grdLookUpCapitalAccount.Properties.Columns[column.ColumnName].Visible = false;
                        grdLookUpDepreciationAccount.Properties.Columns[column.ColumnName].Visible = false;
                        grdLookUpOrgPriceAccount.Properties.Columns[column.ColumnName].Visible = false;
                    }

                }
                grdLookUpCapitalAccount.Properties.DisplayMember = "AccountCode";
                grdLookUpCapitalAccount.Properties.ValueMember = "AccountCode";

                grdLookUpDepreciationAccount.Properties.DisplayMember = "AccountCode";
                grdLookUpDepreciationAccount.Properties.ValueMember = "AccountCode";

                grdLookUpOrgPriceAccount.Properties.DisplayMember = "AccountCode";
                grdLookUpOrgPriceAccount.Properties.ValueMember = "AccountCode";

            }
        }

        #endregion
        public FrmXtraFixedAssetCategoryDetail()
        {
            InitializeComponent();
            _fixedAssetCategoryPresenter = new FixedAssetCategoryPresenter(this);
            _fixedAssetCategoriesPresenter = new FixedAssetCategorysPresenter(this);
            _accountsPresenter = new AccountsPresenter(this);
        }

        #region Override Methods

        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected override void InitData()
        {
            if (KeyValue != null)
            {
                _fixedAssetCategoryPresenter.Display(KeyValue);
                _fixedAssetCategoriesPresenter.DisplayForComboTree(int.Parse(KeyValue));
                grdLockUpParentID.Enabled = false;
                _accountsPresenter.GetAll();
            }
            else
            {
                _fixedAssetCategoriesPresenter.GetAll();
                _accountsPresenter.GetAll();
            }

        }

        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected override void InitControls()
        {
            txtFixedAssetCategoryCode.Focus();
            if ((ActionMode == ActionModeEnum.Edit) && HasChildren) grdLockUpParentID.Properties.ReadOnly = true;
            else grdLockUpParentID.Properties.ReadOnly = false;
        }

        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns></returns>
        protected override bool ValidData()
        {
            if (string.IsNullOrEmpty(FixedAssetCategoryCode))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResAccountCode"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtFixedAssetCategoryCode.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(FixedAssetCategoryName))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResAccountName"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtFixedAssetCategoryName.Focus();
                return false;
            }
            if (grdLockUpParentID.Text == txtFixedAssetCategoryCode.Text)
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResCodeSameAsParentError"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                grdLockUpParentID.Focus();
                return false;
            }

            //if (ActionMode == ActionModeEnum.AddNew && CheckCodeExisted())
            //{
            //    XtraMessageBox.Show(string.Format(ResourceHelper.GetResourceValueByName("ResFixedAssetCategoryAlreadyExists"), FixedAssetCategoryCode), ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    txtFixedAssetCategoryCode.Focus();
            //    return false;
            //}

            return true;
        }

        //private bool CheckCodeExisted()
        //{
        //    var listFixedAssetCategories = _fixedAssetCategoriesPresenter.GetAll();
        //    if (listFixedAssetCategories.Count > 0)
        //    {
        //        foreach (var fixedAssetCategoryModel in listFixedAssetCategories)
        //        {
        //            if (fixedAssetCategoryModel.FixedAssetCategoryCode == FixedAssetCategoryCode)
        //            {
        //                return true;
        //            }
        //        }
        //    }
        //    return false;
        //}

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected override string SaveData()
        {
            //string i;
            //try
            //{
                return _fixedAssetCategoryPresenter.Save();
            //}
            //catch (Exception ex)
            //{
            //    i = 0;
            //    if (ex.Message.Contains("IX_FixedAssetCategory_FixedAssetCategoryCode"))
            //    {
            //        XtraMessageBox.Show(string.Format(ResourceHelper.GetResourceValueByName("ResFixedAssetCategoryAlreadyExists"), FixedAssetCategoryCode), ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            //        txtFixedAssetCategoryCode.Focus();
            //    }
            //    else
            //    {
            //        XtraMessageBox.Show(ex.Message, ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    }
            //}

            //return i;
        }

        #endregion

        private void grdLockUpParentID_KeyDown(object sender, KeyEventArgs e)
        {
            if (grdLockUpParentID.SelectionLength == grdLockUpParentID.Text.Length && (e.KeyData == Keys.Back || e.KeyData == Keys.Delete))
            {
                grdLockUpParentID.EditValue = null;
                e.Handled = true;
            }
        }
    }
}
